import router from './router'
import store from './store'
import { Message} from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from './utils/auth'
NProgress.configure({
  showSpinner: false
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  //cookie是否有token存在
  if (getToken()) {
    if (to.path === 'location.href = `/login`') {
      next({
        path: '/'
      })
      NProgress.done()
    } else {
      // 角色是否有权限
      if (store.getters.roles.length === 0) {
        Message({
          message: "登录过期，请重新登录!",
          type: 'error',
          duration: 1500,
        })
        setTimeout(() => {
          location.href = `/login`
        }, 1500)
      } else {
        next()
      }
    }
  } else {
   Message({
     message: "登录过期，请重新登录!",
     type: 'error',
     duration: 1500,
   })
   setTimeout(() => {
     location.href = `/login`
   }, 1500)
    NProgress.done()
  }
})

router.afterEach(() => {
  NProgress.done()
})
